<?php

namespace app\api\controller\wanlshop;

use WanlPay\Supports\Str;
use app\admin\service\Alipay;
use app\api\model\WpsGoods;
use app\api\model\WpsNotice;
use app\api\model\WpsSloveNotice;
use app\api\service\wanlshop\Goods;
use fast\Http;
use think\Exception;
use think\Log;
use think\Request;
use app\common\controller\Api;
use WanlPay\Yansongda\Pay;

class Test extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $wpsGoodsModel = null;
    protected $wpsNoticeModel = null;
    protected $wpsSloveNoticeModel = null;
    // 商品服务
    protected $goodsService = null;

    protected $config = null;
    protected $alipayconfig = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->wpsGoodsModel = new WpsGoods();
        $this->wpsNoticeModel = new WpsNotice();
        $this->wpsSloveNoticeModel = new WpsSloveNotice();
        $this->goodsService = new Goods();
        $this->config = [
//            'app_id' => '2017122201077772',
            'app-id' => '9021000126635851',
            'notify_url' => 'http://yansongda.cn/notify.php',
            'return_url' => 'http://yansongda.cn/return.php',
            'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnbYVsIkj/37jO6fNystAz8yel5gEJxr2dcrwoCLKwQsgkj7qTTszrfVTPY8sY5AubNQ/uJErt4lfrTTvatH+3862zdbzBK9YbAAPhpQeF0ZlngvNxW8xsPrOdO3W2qfV+ItwSfsR9gCrRQ+VQc4Wia9kYcGhr9izG6Pc14DCDxmk+M6ZH2X24NpCM5ZUfmaXcPjOo0PByT5RXgOj1BjYzFIWQE/B5dFEJNIIxHN19a8Ytq4vuYD3XheXSLF4ZcAVQksQogxWs+ZwXK7AGdPL0hUIbdiV1HR2uYE1JMLsA3aeIlUtFdjbFOu7Ky+7h9+oNtBI8q1d25/wRaorkuy+RwIDAQAB',
            // 加密方式： **RSA2**
            'private_key' => 'MIIEowIBAAKCAQEAnY2TouMc2FfE59yKxEHTnhb5yUr9+cEG6vk+naMMzlyKL5+L2dhOI8y4F6D7A35+y31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bmH/wf8D0ZRqjTVUV+EWpG9bYQ0xr2VBgbwqGNOwnaoivifoup7D6qs4JuRII/DIuuSGVi5gt/QKBgQCPC6ouNSQZBRMjlMo/D7RgfdmioCScmUv7BUomu29FcqI2733Y4rxB401OKPXSe9md3eyp4mSkgpLzqh0zucp7hGdMiUyruQvn6bcNRd0XHupzSs3b3JgBcSiOQWnNu3nTNXRM41v/ImV6dQvMBuVVlPwd5fc4s8kdx4N4KmkjdQKBgDF/MvtWQcHLo3pXFkgIrdGd99AIyXwUdzBt1BXgdB04cl8T0nzbBIKEHdLU2XNjgYc8H5I5txT9WzAAv+AtxPKgMHL8jrNod1N1Ff15dMJifnDwwFA5lfamrDixv5xMUCnhn6jPoaOEDC8I6M7d4PA8GJoBGXPGs+WSUXEwhDyK',
            'log' => [ // optional
                'file' => './logs/alipay.log',
                'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
                'type' => 'single', // optional, 可选 daily.
                'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
            ],
            'http' => [ // optional
                'timeout' => 5.0,
                'connect_timeout' => 5.0,
                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
            ],
            'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
        ];

        $config = get_addon_config('wanlshop');
        $pay_config = [];
        $pay_config = [
            'app_id' => $config['sdk_alipay']['app_id'],
            'notify_url' => $config['ini']['appurl'] . $config['sdk_alipay']['notify_url'],
            //'return_url' => $config['ini']['appurl'] . $config['sdk_alipay']['return_url'],
            'private_key' => $config['sdk_alipay']['private_key'],
            'log' => [
                'file' => LOG_PATH . 'wanlpay' . DS . 'alipay' . '-' . date("Y-m-d") . '.log',
                'level' => 'debug', // todo 建议生产环境等级调整为 info，开发环境为 debug
                'type' => 'single', // optional, 可选 daily.
                'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
            ],
            'http' => [
                'timeout' => 5.0,
                'connect_timeout' => 5.0
            ],
        ];
        if (isset($config['sdk_alipay']['app_cert_public_key']) && substr($config['sdk_alipay']['app_cert_public_key'], 0, 8) == '/addons/') {
            $pay_config['app_cert_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['sdk_alipay']['app_cert_public_key'], 1));
        }
        if (isset($config['sdk_alipay']['alipay_root_cert']) && substr($config['sdk_alipay']['alipay_root_cert'], 0, 8) == '/addons/') {
            $pay_config['alipay_root_cert'] = ROOT_PATH . str_replace('/', DS, substr($config['sdk_alipay']['alipay_root_cert'], 1));
        }
        if (isset($config['sdk_alipay']['ali_public_key']) && (Str::endsWith($config['sdk_alipay']['ali_public_key'], '.crt') || Str::endsWith($config['sdk_alipay']['ali_public_key'], '.pem'))) {
            $pay_config['ali_public_key'] = ROOT_PATH . str_replace('/', DS, substr($config['sdk_alipay']['ali_public_key'], 1));
        }

        $this->alipayconfig = $pay_config;
    }

    public function index()
    {
        echo 123;die;
        set_time_limit(0);
        for ($i = 1; $i < 30; $i++) {
            $params = [
                'pageNo' => $i,
                'pageSize' => 50,
                'goodName' => "周六福S925银和田白玉白玉佛吊坠ZLFY04985"
            ];

            // header
            $header = [
                'channelType:' . 'DONGHUOMU',
                'md5:' . $this->getSign($params)
            ];

            // 额外参数
            $option = [
                CURLOPT_HTTPHEADER => $header,
                CURLOPT_POSTFIELDS => http_build_query($params),
                CURLOPT_POST => 1
            ];

            $resp = Http::post("https://api.weipinshang.net/mcang/Mcang/getGoodsList", $params, $option);

            // 添加数据到数据库
            $list = json_decode($resp, true);
            if (isset($list['data']['list']) && !empty($list['data']['list'])) {
                $al = $this->goodsService->insGoods($list['data']['list']);
                if ($al) {
                    //$this->success("成功", $al, 1);
                }
            }
        }

        $this->error("失败", null, 0);
    }

    public function admin()
    {
        $order = [
            'out_biz_no' => 'qqqq'.time(),
            'trans_amount' => 0.1,
            'product_code' => 'TRANS_ACCOUNT_NO_PWD',
            'biz_scene' => 'DIRECT_TRANSFER',
            'payee_info' => [
                'identity' => 'xxm1234@126.com',
                'identity_type' => 'ALIPAY_LOGON_ID',
                'name' => '江苏孝源医疗器械有限公司'
            ],
        ];
        $result = Pay::alipay($this->alipayconfig)->transfer($order);
        dump($result);die;
        return Pay::alipay(self::getConfig('alipay'))->transfer($order);
        return response($result);
    }

    public function admin2()
    {
        $alipay = Pay::alipay($this->config);
        $order = [
            'out_trade_no' => '20230830123321',
        ];
        $result = $alipay->find($order);
        return $result;
    }

    public function notify()
    {
        try {
            $data = $this->alipay->verify();
            $this->success($data);
            Log::info('Alipay notify:' . json_encode($data->all()));
        } catch (Exception $e) {
            var_dump(123);
        }
    }

    public function callback()
    {
        Log::info('callback');
    }
}